<?php
namespace PsiFramework\Applications\AlloMediaPortal\Classes\Services;

import("Classes.Services.AbstractService");

use \PsiFramework\Classes\Factory\DAOFactory;
use \PsiFramework\Classes\Services\AbstractService;
use \PsiFramework\Classes\Util\HTTP\Request;

/**
 * Bibliotheque
 * Gestion de ma bibliothèque
 *
 * @author Rémi San
 * @version beta
 */

/**
 * Service de listing des auteurs
 *
 * @package Services
 */
class CheckUserUnicityService extends AbstractService
{

    /**
     * Constructor
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * The main function of the method which is called every time
     * an service is created by the front controller.
     *
     * @param $request      the HTTP request to be treated by the action
     */
    public function execute(Request $request)
    {
        parent::execute($request);

        $userId = $request->getParameter("user_id");
        if (isNullOrEmptyString($userId)) {
        	$userId = null ;
        }
        $login = $request->getParameter("login");
        $email = $request->getParameter("email");

        $userDao = DAOFactory::getDAO("User");

        if (!isNullOrEmptyString($login)) {
            $user = $userDao->retrieveByLogin($login, $userId);
        } else if (!isNullOrEmptyString($email)) {
            $user = $userDao->retrieveByEmail($email, $userId);
        }

        $trueFalse = 'true' ;
        if (is_null($user)) {
        	$trueFalse = 'false' ;
        }

        $objects = new \stdClass();
        $objects->exists = !(is_null($user));
        $this->_response->setAttribute("objects", $objects);
        $this->show('json');
    }
}
